package com.smartwebx.ha.web.gis.module.screen;

import static com.smartwebx.ha.web.admin.common.HaConstant.HA_USER_SESSION_KEY;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.smartwebx.ha.biz.assign.HaAreaManager;
import com.smartwebx.ha.biz.rainwater.feature.FeatureRainfallManager;
import com.smartwebx.ha.biz.rainwater.real.LayerRainfallManager;
import com.smartwebx.ha.biz.rainwater.real.RainfallManager;
import com.smartwebx.ha.dal.annot.JsonModule;
import com.smartwebx.ha.dal.annot.JsonModules;
import com.smartwebx.ha.dal.assign.bean.HaArea;
import com.smartwebx.ha.dal.usebean.DateValue;
import com.smartwebx.ha.dal.usebean.DateValueTotal;
import com.smartwebx.ha.dal.usebean.FeatureRainSlideDay;
import com.smartwebx.ha.dal.usebean.GisRainLayerRainfall;
import com.smartwebx.ha.dal.usebean.RainSlideDay;
import com.smartwebx.ha.dal.usebean.RainSlideHour;
import com.smartwebx.ha.dal.usebean.view.ViewRainSlideDay;
import com.smartwebx.ha.dal.usebean.view.ViewRainSlideHour;
import com.smartwebx.ha.web.admin.common.HaUserSys;
import com.smartwebx.ha.web.gis.module.screen.BasicScreen;
import com.smartwebx.ha.web.util.json.module.DateModule;
import com.smartwebx.ha.web.util.json.module.DateSerializer;
import com.smartwebx.ha.web.util.json.module.DoubleModule;
import com.smartwebx.ha.web.util.json.module.DoubleSerializer;

/**
 * GIS 雨情
 * 
 * @author zhangxi
 * 
 */
public class RainfallScreen extends BasicScreen {
	@Autowired
	private RainfallManager rainfallManager;
	@Autowired
	private LayerRainfallManager layerRainfallManager;
	@Autowired
	private FeatureRainfallManager featureRainfallManager;
	@Autowired
	private HaAreaManager haAreaManager;
	@Autowired
	private HttpSession session;
	@Autowired
	private HttpServletRequest request;

	class DateValueTotalFilter {
		@JsonSerialize(using = DateSerializer.class)
		public Date tm;
		@JsonSerialize(using = DoubleSerializer.class)
		public Double val;
	}

	class GISRainDataFilter {
		@JsonSerialize(using = DoubleSerializer.class)
		public Double rainFallVal;

		@JsonSerialize(using = DoubleSerializer.class)
		public Double warValue;
	}

	/**
	 * 单站某时间段小时累计降雨过程（chart服务，暂不处理 by zx）
	 * 
	 * @param stcd
	 *            降雨站码
	 * @param bTm
	 *            开始时间
	 * @param eTm
	 *            结束时间
	 * @return Map<String, Object>
	 * @throws ParseException
	 */
	@JsonModules({
			@JsonModule(clazz = DateModule.class, value = "MM-dd HH:00"),
			@JsonModule(clazz = DoubleModule.class, value = "1") })
	public List<DateValueTotal> getDayHourTotalRainProcess(String stcd,
			Date bTm, Date eTm) throws IOException {
		List<DateValueTotal> dayHourTotalRainProcessList = rainfallManager
				.getSingleHourTotalRainProcess(stcd, bTm, eTm);

		return dayHourTotalRainProcessList;
	}

	/**
	 * 获得GIS雨量与面雨量
	 * 
	 * @param bTm
	 * @param eTm
	 * @return
	 * @throws IOException
	 */
	@JsonModule(clazz = DoubleModule.class, value = "1")
	public GisRainLayerRainfall getGisRainLayerRainfall(Date bTm, Date eTm,
			String type) throws IOException {
		HaUserSys loginUser = (HaUserSys) session.getAttribute("haUser");
		HaArea area = haAreaManager.findOne(loginUser.getAreaId());
		return rainfallManager.getGisRainLayerRainfall(bTm, eTm, type, area);
	}

	private int getAreaId() {
		HttpSession session = request.getSession();
		HaUserSys haUserSys = (HaUserSys) session
				.getAttribute(HA_USER_SESSION_KEY);
		Integer areaId = haUserSys.getAreaId();
		return areaId;
	}

	/**
	 * 所有站时段累计降雨(zx)
	 * 
	 * @param bTm
	 * @param eTm
	 * @return
	 */
	// @JsonModule(clazz = DoubleModule.class, value = "1")
	// public List<GisRain> getRainGisInfo(Date bTm, Date eTm) throws
	// IOException {
	// List<GisRain> gisRainDatas = rainfallManager
	// .getAllStatRainVal(bTm, eTm);
	//
	// return gisRainDatas;
	// }

	/**
	 * 日降雨过程(zx)
	 * 
	 * @param stcd
	 * @param bTm
	 * @param eTm
	 * @return
	 */
	@JsonModules({ @JsonModule(clazz = DateModule.class, value = "yyyy-MM-dd"),
			@JsonModule(clazz = DoubleModule.class, value = "1") })
	public List<DateValueTotal> getDayProcess(String stcd, Date bTm, Date eTm)
			throws IOException {
		List<DateValueTotal> dayTotalRainProcessList = rainfallManager
				.getSingleDayTotalRainProcess(stcd, bTm, eTm);

		return dayTotalRainProcessList;
	}

	@JsonModule(clazz = DoubleModule.class, value = "1", isNumber = true)
	public Object getDayProcessByGisJs(String stcd, Date bTm, Date eTm) {
		List<DateValueTotal> dayTotalRainProcessList = rainfallManager
				.getSingleDayTotalRainProcess(stcd, bTm, eTm);
		List<Double> vals = Lists.newArrayList();
		List<Double> totals = Lists.newArrayList();
		for (DateValueTotal valTotal : dayTotalRainProcessList) {
			vals.add(valTotal.getVal());
			totals.add(valTotal.getTotalVal());
		}

		Map<String, List<Double>> result = ImmutableMap.of("rainVals", vals,
				"rainTotVals", totals);
		return result;
	}

	/**
	 * 时降雨过程(zhangxi)
	 * 
	 * @param stcd
	 * @param bTm
	 * @param eTm
	 * @return
	 * @throws ParseException
	 * @throws IOException
	 */
	@JsonModules({
			@JsonModule(clazz = DateModule.class, value = "yyyy-MM-dd HH:mm"),
			@JsonModule(clazz = DoubleModule.class, value = "1") })
	public List<DateValueTotal> getHourProcess(String stcd, Date bTm, Date eTm)
			throws IOException {
		List<DateValueTotal> hourTotalRainProcessList = rainfallManager
				.getSingleHourTotalRainProcess(stcd, bTm, eTm);

		return hourTotalRainProcessList;
	}

	@JsonModule(clazz = DoubleModule.class, value = "1", isNumber = true)
	public Object getHourProcessByGisJs(String stcd, Date bTm, Date eTm) {
		List<DateValueTotal> hourTotalRainProcessList = rainfallManager
				.getSingleHourTotalRainProcess(stcd, bTm, eTm);

		List<Double> vals = Lists.newArrayList();
		List<Double> totals = Lists.newArrayList();
		for (DateValueTotal valTotal : hourTotalRainProcessList) {
			vals.add(valTotal.getVal());
			totals.add(valTotal.getTotalVal());
		}

		Map<String, List<Double>> result = ImmutableMap.of("rainVals", vals,
				"rainTotVals", totals);
		return result;
	}

	/**
	 * 日降雨滑动分析
	 * 
	 * @param stcd
	 * @param bTm
	 * @param eTm
	 * @return
	 * @throws InvocationTargetException
	 * @throws IllegalAccessException
	 * @throws ParseException
	 */
	@JsonModules({ @JsonModule(clazz = DateModule.class, value = "yyyy-MM-dd"),
			@JsonModule(clazz = DoubleModule.class, value = "1") })
	public ViewRainSlideDay getRainSlideDay(String stcd, Date bTm, Date eTm)
			throws IllegalAccessException, InvocationTargetException {
		List<DateValue> dateValueList = rainfallManager
				.getSingleStatDayProcess(stcd, bTm, eTm);
		RainSlideDay rainSlideDay = layerRainfallManager.getRainSlideDay(stcd,
				dateValueList);
		FeatureRainSlideDay featureRainSlideDay = featureRainfallManager
				.getRainSlideDay(stcd);
		ViewRainSlideDay viewRainSlideDay = new ViewRainSlideDay(
				featureRainSlideDay.getOneDay(),
				featureRainSlideDay.getThreeDay(),
				featureRainSlideDay.getSevenDay(),
				featureRainSlideDay.getFifteenDay(),
				featureRainSlideDay.getThirtyDay());
		BeanUtils.copyProperties(viewRainSlideDay, rainSlideDay);

		return viewRainSlideDay;
	}

	/**
	 * 时降雨过程滑动分析(zhangxi)
	 * 
	 * @param stcd
	 * @param bTm
	 * @param eTm
	 * @return
	 * @throws InvocationTargetException
	 * @throws IllegalAccessException
	 * @throws ParseException
	 */
	@JsonModules({ @JsonModule(clazz = DateModule.class, value = "yyyy-MM-dd"),
			@JsonModule(clazz = DoubleModule.class, value = "1") })
	public ViewRainSlideHour getRainSlideHour(String stcd, Date bTm, Date eTm)
			throws IllegalAccessException, InvocationTargetException {
		// 小时雨量过程
		List<DateValue> dateValues = rainfallManager.getSingleStatHourProcess(
				stcd, bTm, eTm);
		// 对此站的小时过程数据进行滑动分析
		RainSlideHour rainSlideHour = layerRainfallManager.getRainSlideHour(
				stcd, dateValues);
		FeatureRainSlideDay featureRainSlideDay = featureRainfallManager
				.getRainSlideDay(stcd);

		ViewRainSlideHour viewRainSlideHour = new ViewRainSlideHour(
				featureRainSlideDay.getOneHour(),
				featureRainSlideDay.getThreeHour(),
				featureRainSlideDay.getSixHour(),
				featureRainSlideDay.getTwelveHour(),
				featureRainSlideDay.getTwentyFourHour());
		BeanUtils.copyProperties(viewRainSlideHour, rainSlideHour);

		return viewRainSlideHour;
	}

}
